package com.student.dms;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter4.BaseQuickAdapter;
import com.chad.library.adapter4.QuickAdapterHelper;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.luck.picture.lib.utils.ToastUtils;
import com.student.db.AccountDao;
import com.student.db.AccountEntity;
import com.student.db.AppDatabase;
import com.student.db.FixReportEntity;
import com.student.db.PaymentDao;
import com.student.db.PaymentEntity;
import com.student.dms.databinding.ActivityPaymentHistoryBinding;
import com.student.dms.databinding.ActivityRegisterBinding;
import com.student.dms.databinding.ItemFixHistoryBinding;
import com.student.dms.databinding.ItemPaymentHistoryBinding;

import java.text.SimpleDateFormat;
import java.util.Date;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class ActivityPaymentHistory extends BaseActivity{

    private  ActivityPaymentHistoryBinding activityPaymentHistoryBinding;
    private AppDatabase roomDatabase;
    private AccountDao accountDao;
    private AccountEntity accountEntity;
    private BaseQuickAdapter<PaymentEntity, VH> baseQuickAdapter;
    private PaymentDao paymentDao;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityPaymentHistoryBinding = ActivityPaymentHistoryBinding.inflate(getLayoutInflater());
        setContentView(activityPaymentHistoryBinding.getRoot());
        activityPaymentHistoryBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(TitleBar titleBar) {
                finish();
            }
        });
        SharedPreferences sharedPreferences =getSharedPreferences("user", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", "");
        roomDatabase = DMSApplication.getApplication().getRoomDatabase();
        accountDao = roomDatabase.provideAccountDao();
        paymentDao = roomDatabase.providePaymentDao();
        Single<AccountEntity> accountFlow = accountDao.searchAccountByName(username);
        disposable.add(accountFlow.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(accountEntity -> {
            this.accountEntity =accountEntity;
        }, studentFlowThrowable -> {
            ToastUtils.showToast(ActivityPaymentHistory.this, "用户不存在，请注册！");
        }));
        activityPaymentHistoryBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(TitleBar titleBar) {
                finish();
            }
        });
        baseQuickAdapter = new BaseQuickAdapter<PaymentEntity, VH>() {
            @NonNull
            @Override
            protected VH onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
                VH vh = new VH(ItemPaymentHistoryBinding.inflate(LayoutInflater.from(context), viewGroup, false));
                return vh;
            }

            @Override
            protected void onBindViewHolder(@NonNull VH vh, int position, @Nullable PaymentEntity itemData) {
                vh.itemViewBidnng.name.setText("姓名：      " + itemData.getName());
                vh.itemViewBidnng.roomNo.setText("寝室号：   " + itemData.getRoomNo());
                String type;
                if(itemData.getPayType()==1){
                    type="水费";
                }else{
                    type="电费";
                }
                vh.itemViewBidnng.payType.setText("缴费类型：" + type);
                vh.itemViewBidnng.payNum.setText("缴费金额：" + itemData.getPayment());
                vh.itemViewBidnng.payTime.setText("缴费时间：" + itemData.getPayTime());
                vh.itemViewBidnng.payStatusTime.setText("审核时间：" + itemData.getPayStatusTime());

                String status;
                if(itemData.getPayStatus()==1){
                    status="已审核";
                    vh.itemViewBidnng.submit.setVisibility(View.GONE);
                }else{
                    status="未审核";
                    vh.itemViewBidnng.submit.setVisibility(View.VISIBLE);
                }
                vh.itemViewBidnng.payStatus.setText("审核状态：" +status);

                if(accountEntity!=null && accountEntity.getType()==1){
                    vh.itemViewBidnng.submit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                            String time = simpleDateFormat.format(new Date());
                            itemData.setPayStatus(1);
                            itemData.setPayStatusTime(time);
                            //点击报修
                            disposable.add(paymentDao.insert(itemData).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(() -> {
                                        ToastUtils.showToast(ActivityPaymentHistory.this, "审批成功！");
                                        baseQuickAdapter.notifyDataSetChanged();
                                    }
                            ));
                        }
                    });
                }

            }
        };
        QuickAdapterHelper build = new QuickAdapterHelper.Builder(baseQuickAdapter).build();
        activityPaymentHistoryBinding.recyclerView.setAdapter(build.getAdapter());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ActivityPaymentHistory.this);
        activityPaymentHistoryBinding.recyclerView.setLayoutManager(linearLayoutManager);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(ActivityPaymentHistory.this, DividerItemDecoration.VERTICAL);
        activityPaymentHistoryBinding.recyclerView.addItemDecoration(dividerItemDecoration);
        disposable.add(paymentDao.getPayment().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(fixReportEntities -> {
                    baseQuickAdapter.submitList(fixReportEntities);
                }, error -> {

                }
        ));

    }


    static class VH extends RecyclerView.ViewHolder {
        @NonNull
        public ItemPaymentHistoryBinding itemViewBidnng;

        public VH(@NonNull ItemPaymentHistoryBinding itemViewBidnng) {
            super(itemViewBidnng.getRoot());
            this.itemViewBidnng = itemViewBidnng;
        }
    }
}
